/*
 * ViJxtaCommand.java
 *
 * Created on April 15, 2005, 8:17 AM
 */

package net.jxta.myjxta.plugins.vijxta;

import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredTextDocument;
import net.jxta.endpoint.TextDocumentMessageElement;
import net.jxta.myjxta.dialog.Dialog;
import net.jxta.myjxta.dialog.DialogManager;
import net.jxta.myjxta.dialog.DialogMessage;
import net.jxta.myjxta.dialog.commands.AbstractCommand;
import net.jxta.myjxta.util.Group;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import java.util.logging.Level;
import net.jxta.logging.Logging;
import java.util.logging.Logger;

/**
 * Command representing ViJxta video chat. Sendss a response to a command
 *  initiated on CommandPipe
 *
 * @author jamoore
 */
public class ViJxtaCommand extends AbstractCommand{
    
   public static final String ID = ViJxtaCommand.class.getName();       
    public static final String PIPE = "ViJxtaPipe";
    private static final Logger LOG = Logger.getLogger(ViJxtaCommand.class.getName());
    
    public String getId () {
        return ID;
    }
    
    public boolean process (Dialog dialog, DialogMessage msg) {
        LOG.setLevel(Level.INFO);
        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
            LOG.info("process: " + (msg != null ? msg.toString()
                                                : null));
        }        
        String cmd = msg != null ? msg.getCommand () : null;
        Group g = dialog.getGroup ();
        PeerGroup pg = g.getPeerGroup ();
        DialogMessage dm = new DialogMessage (pg.getPeerName (), null,
                pg.getPeerGroupID ().toString (), pg.getPeerGroupName ());
        
        if (cmd != null &&
                cmd.length () > 0) {
            
            if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
                LOG.info("processing command: " + cmd);
            }
            
            setCommand (cmd);
            
            //get pipe adv from DialogManager
            StructuredDocument d = getPipeAdv (g);
            //attach it as a message element
            TextDocumentMessageElement te = d != null ?
                new TextDocumentMessageElement (PIPE,
                    (StructuredTextDocument)d, null) :
                null;
            
            if (te != null) {
                setReply ("pipe:" + PIPE);
                setStatus (true);
                
                dm.setCommand (getResponse ());
                dm.addMessageElement (PIPE, te);
                
                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
                    LOG.info("results: " + dm);
                }
            } else {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.severe("can\'t construct document");
                }
            }            
        } else {
            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                LOG.severe("invalid command: " + cmd);
            }
        }
        
        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
            LOG.info("dispatching: " + (dm != null ? dm.toString()
                                                   : null));
        }
        
        dialog.dispatch (dm);
        
        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
            LOG.info("dispatching: " + getStatus());
        }
        //LOG.setLevel(Level.SEVERE);
        return getStatus ();
    }
    
    private StructuredDocument getPipeAdv (Group g) {
        if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {
            LOG.info("getPipeAdvl");
        }
        
        String pn = Dialog.getDialogNamer (ViJxtaDialog.class).
                getDialogName (g.getPeerGroup ().getPeerName ());
        PipeAdvertisement pa = DialogManager.getInstance (g, pn,
                PipeService.UnicastType).getPipeAdv (g.getPeerGroup ());
        
        return (StructuredDocument)pa.getDocument (MimeMediaType.XMLUTF8);
    }

    
}
